﻿using com.ruovea.utilty.Appseting;
using SqlSugar;

namespace com.ruovea.gogs.service
{
    /// <summary>
    /// 数据库服务工厂
    /// </summary>
    public class ctx
    {
        public static string ConnStr;

        /// <summary>
        /// 数据库类型 GogsDb DbType
        /// </summary>
        private static string dbType { get; set; }
        /// <summary>
        /// 数据连接 GogsDb  ConnectionString
        /// </summary>
        private static string dbConnectionString { get; set; }

        public static SqlSugarClient DB
        {
            get
            {
                dbConnectionString = Appsettings.app("GogsDb", "ConnectionString")?.ToString();
                dbType = Appsettings.app("GogsDb", "DbType")?.ToString();

                #region sqlSourceType
                DbType sqlSourceType = DbType.Sqlite;
                switch (dbType)
                {
                    case "SqlServer":
                        sqlSourceType = DbType.SqlServer;
                        break;
                    case "SqLite":
                        sqlSourceType = DbType.Sqlite;
                        break;
                    case "MySql":
                        sqlSourceType = DbType.MySql;
                        break;
                    case "Oracle":
                        sqlSourceType = DbType.Oracle;
                        break;
                    case "PostgreSQL":
                        sqlSourceType = DbType.PostgreSQL;
                        break;
                }
                #endregion

                var ty = new ConnectionConfig()
                {
                    ConnectionString = dbConnectionString,//必填, 数据库连接字符串
                    DbType = sqlSourceType,         //必填, 数据库类型
                    IsAutoCloseConnection = true,       //默认false, 时候知道关闭数据库连接, 设置为true无需使用using或者Close操作
                    InitKeyType = InitKeyType.SystemTable,    //默认SystemTable, 字段信息读取, 如：该属性是不是主键，是不是标识列等等信息
                };
                return new SqlSugarClient(ty);
            }
        }
    }

    public class DbConfig
    {
        /// <summary>
        /// 链接字符串
        /// </summary>
        public string ConnectionString { get; set; }
        /// <summary>
        /// 数据库类型
        /// </summary>
        public string DbType { get; set; }
    }

}
